package com.aaa.filter;

import cn.hutool.core.util.ObjectUtil;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author ：肖成杰（1752167034@qq.com）
 * @date ：Created in 2022/8/8 14:26
 * @description：
 * @modified By：
 * @version:
 */
//@WebFilter("/*")
public class LogFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        //登录页面需要放行
        String requestURI = req.getRequestURI();
        // 合法的用户，放行， chain.doFilter();，
        String contextPath = req.getContextPath();
        HttpSession session = req.getSession();
        Object username = session.getAttribute("username");

        if(ObjectUtil.isNotEmpty(username)||requestURI.contains("login")){

            chain.doFilter(req,res);
        }else
        {

            //非法用户，滚去登录
            res.sendRedirect(contextPath+"views/login.jsp");
        }
    }
}